package com.nativetalk.controller.app.three.teacher.reserve;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.three.reserve.search.ThreeReserveSearchService;
import com.nativetalk.service.three.reserve.time.insert.ThreeReserveTimeInsertService;
import com.nativetalk.service.three.reserve.time.search.ThreeReserveTimeSearchService;
import com.nativetalk.service.three.reserve.update.ThreeReserveUpdateService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeTeacherReserveController {
    private static Logger logger = Logger.getLogger(ThreeTeacherReserveController.class);
    @Resource
    private ThreeReserveTimeSearchService threeReserveTimeSearchService;
    @Resource
    private ThreeReserveTimeInsertService threeReserveTimeInsertService;
    @Resource
    private ThreeReserveSearchService threeReserveSearchService;
    @Resource
    private ThreeReserveUpdateService threeReserveUpdateService;

    /**
     * @return RetInfo
     * @Purpose 把某段时间设置为可预约或不可预约
     * @version 3.1
     * @time 2015-07-19
     * @author lizhun
     */
    @RequestMapping(value = "/app/7/teatoken/teacher/reserve/time/set", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo set(@RequestBody Map<String, Integer> map,
                       @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info(retInfo.toString());
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            RetInfo retInfo = threeReserveTimeInsertService.setReserveTime(tdNtTeacherResult.getTea_id(), map.get("week"), map.get("is_valid"), map.get("reserve_time"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查找周几用户已设置的可预约时间段
     * @version 3.1
     * @time 2015-07-19
     * @author lizhun
     */
    @RequestMapping(value = "/app/7/teatoken/teacher/reserve/time/find", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo find(@RequestBody Map<String, Integer> map,
                        @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info(retInfo.toString());
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            RetInfo retInfo = threeReserveTimeSearchService.findTeacherSetReserveTime(tdNtTeacherResult.getTea_id(), map.get("week"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }

    /**
     * @return RetInfo
     * @Purpose 取消订单
     * @version 3.1
     * @time 2015-07-19
     * @author lizhun
     */
    @RequestMapping(value = "/app/7/teatoken/teacher/reserve/cancel", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo order(@RequestBody Map<String, Integer> map) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            retInfo = threeReserveUpdateService.cancelTeaId(map.get("reserve_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }

    /**
     * @return RetInfo
     * @Purpose 未完成预约列表
     * @version 3.1
     * @time 2015-07-19
     * @author lizhun
     */
    @RequestMapping(value = "/app/7/teatoken/teacher/reserve/list/unfinish", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo unfinish(@RequestBody Page page,
                            @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info(retInfo.toString());
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            page.setObj(tdNtTeacherResult.getTea_id());
            RetInfo retInfo = threeReserveSearchService.findTdNtReserveUnFinishByTeaId(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 已完成预约列表
     * @version 3.1
     * @time 2015-07-19
     * @author lizhun
     */
    @RequestMapping(value = "/app/7/teatoken/teacher/reserve/list/finish", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo finish(@RequestBody Page page,
                          @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info(retInfo.toString());
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            page.setObj(tdNtTeacherResult.getTea_id());
            RetInfo retInfo = threeReserveSearchService.findTdNtReserveFinishByTeaId(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
}



